package com.cskaoyan.market.shiro;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName WxRealm
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2024/3/28 15:43
 * @Version V1.0
 **/
@Component
public class WxRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String username = usernamePasswordToken.getUsername();
        MarketUser user = userService.selectByUsername(username);
        String password = null;
        if(user != null){
            password = user.getPassword();
        }
        //为了防止出现空指针
        return new SimpleAuthenticationInfo(user, password, getName());
        //return null;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }


}
